package com.liveneo.stoat.sys.web;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributesModelMap;

import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.liveneo.stoat.common.page.PageParam;
import com.liveneo.stoat.common.web.BaseController;
import com.liveneo.stoat.sys.entity.PrpmRole;
import com.liveneo.stoat.sys.service.RoleService;
/**
 * 
 *@Description: 角色管理
 *@Author:hjt 
 *@Since:2017年1月13日 下午9:47:45 
 *@Version:1.0.0
 */
@Controller
@RequestMapping ("/sys/role")
public class RoleController extends BaseController{
	private static String BASE_PATH = "sys/role";
	@Resource
	private RoleService roleService;
	
	@RequestMapping (value = "/list")
	public String list(PrpmRole prpmRole, ModelMap modelMap, PageParam pageParam){
		logger.info("RoleController.list start");
		PageInfo<PrpmRole> pageInfo = null;
		try {
			pageInfo = roleService.queryListWithPage(prpmRole, pageParam);
			modelMap.put("pageInfo", pageInfo);
			modelMap.put("prpmRole", prpmRole);
		} catch (Exception e) {
			logger.info("RoleController.list error>>>"+e.getMessage());
		}
		logger.info("RoleController.list end");
		return BASE_PATH + "/roleList";
	}
	
	/**  跳转添加/修改用户   */
	@RequestMapping (value = "/toAdd")
	public String toAdd(PrpmRole prpmRole, ModelMap modelMap){
		try {
			if(StringUtils.isNotBlank(prpmRole.getId())){
				prpmRole = roleService.selectById(prpmRole.getId());
			}
		} catch (Exception e) {
			
		}
		modelMap.put("prpmRole", prpmRole);
		return BASE_PATH + "/roleAdd";
	}
	
	/**
	 * 添加修改用户
	 * @param PrpmRole
	 * @return
	 */
	@RequestMapping(value = "/save")
	public String save(PrpmRole prpmRole, BindingResult br, RedirectAttributesModelMap modelMap, String oldLoginName) {
		logger.info("RoleController.save start");
		if (br.hasErrors()) {
            return toAdd(prpmRole, modelMap);
        }
		
		try {
			boolean flag = roleService.save(prpmRole);
			if(flag) {
				putSuccessMsg(modelMap, "保存角色信息成功");
			} else {
				putErrorMsg(modelMap, "保存角色信息失败");
			}
		} catch (Exception e) {
			putErrorMsg(modelMap, "保存角色信息异常");
			logger.error("RoleController.save error>>" + e.getMessage());
		}
		logger.info("RoleController.save end");
		return redirect("redirect:/{}/list", BASE_PATH);
	}
	
	/**
	 * 删除用户
	 * @param PrpmRole
	 * @return
	 */
	@RequestMapping(value = "/del/{id}")
	public String del(@PathVariable String id, RedirectAttributesModelMap modelMap) {
		logger.info("RoleController.del start");
		try {
			boolean flag = roleService.deleteById(id);
			if(flag){
				putSuccessMsg(modelMap, "删除角色成功");
			} else {
				putErrorMsg(modelMap, "删除角色失败");
			}
		} catch (Exception e) {
			putErrorMsg(modelMap, "删除角色异常");
			logger.error("RoleController.del error>>" + e.getMessage());
		}
		logger.info("RoleController.del end");
		return redirect("redirect:/{}/list", BASE_PATH);
	}
	
	/**
	 * 批量删除角色
	 * @param PrpmRole
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value = "/datadel")
	public String datadel(RedirectAttributesModelMap modelMap) {
		logger.info("RoleController.datadel start");
		String res = "-1";
		try {
			List<String> idList = Lists.newArrayList();
			String[] ids = getStrings("ids");
			if(ids != null){
				for(String id : ids){
					idList.add(id);
				}
			}
			boolean flag = false;
			if(idList.size() > 0){
				flag = roleService.deleteByIds(idList);
			}
			if(flag){
				res = "1";
			}
		} catch (Exception e) {
			putErrorMsg(modelMap, "批量删除角色异常");
			logger.error("RoleController.datadel error>>" + e.getMessage());
		}
		logger.info("RoleController.datadel end");
		return res;
	}
}
